/**
 *
 */

import list from './initFactorList.json'
import RuleFactor, { MATCHING_TYPES_BY_DATA_TYPE } from '../model/RuleFactor'
import RuleFactorValue, { VALUE_TYPE_BY_MATCHING_TYPE, VALUE_TYPE } from '../model/RuleFactorValue'

const FACTOR_LIST_KEY = 'FACTOR-LIST-KEY'

const initFactorList = () => {
  console.log('list is:', list)

  localStorage.setItem(FACTOR_LIST_KEY, JSON.stringify(list))
}

const saveData = (data) => {
  localStorage.setItem(FACTOR_LIST_KEY, JSON.stringify(data))
}

const clearFactorList = () => {
  localStorage.removeItem(FACTOR_LIST_KEY)
}

const loadFactorListStr = () => {
  let factorListStr = localStorage.getItem(FACTOR_LIST_KEY)

  if (!factorListStr) {
    factorListStr = list
    saveData(factorListStr)
  }
  return factorListStr
}

const getFactorList = () => {
  const factorListStr = loadFactorListStr()
  const factorListData = JSON.parse(factorListStr)
  const factorList = factorListData.map((factorData) => {
    return new RuleFactor(factorData)
  })

  return factorList
}

const updateFactorList = (factorList) => {
  const str = JSON.stringify(factorList)
  console.log('factor list str is:', str)
}

const getFactorWithCode = (code) => {
  const factorList = getFactorList()
  return factorList.find((factor) => factor.code === code)
}

const updateFactor = (factor) => {
  const factorList = getFactorList()
  const factorIndex = factorList.findIndex((factorItem) => factorItem.code === factor.code)
  factorList.splice(factorIndex, 1, factor)
  saveData(factorList)
}

const getFactorValueWithCode = (factorCode, valueCode) => {
  const factor = getFactorWithCode(factorCode)
  return factor.getValueWithCode(valueCode)
}

const updateFactorValue = (factorCode, factorValue) => {
  const factor = getFactorWithCode(factorCode)
  const valueIndex = factor.values.findIndex((valueItem) => valueItem.code === factorValue.code)
  factor.values.splice(valueIndex, 1, factorValue)
  updateFactor(factor)
}

const addFactorValue = (factorCode, valueData) => {
  const factor = getFactorWithCode(factorCode)
  factor.addValue(valueData)
  updateFactor(factor)
}

const deleteFactorValue = (factorCode, factorValue) => {
  const factor = getFactorWithCode(factorCode)
  const valueIndex = factor.values.findIndex((valueItem) => valueItem.code === factorValue.code)
  factor.values.splice(valueIndex, 1)
  updateFactor(factor)
}

// const addRuleFactor = (factorData) => {
//   const factor = new RuleFactor(factorData)
//   const factorList = getFactorList()
//   factorList.push(factor)
//   saveData(factorList)
// }

const getMatchingTypes = (dataType) => {
  const matchingTypes = MATCHING_TYPES_BY_DATA_TYPE[dataType]
  return matchingTypes
}

const isMultipleValue = (matchingType) => {
  const valueType = VALUE_TYPE_BY_MATCHING_TYPE[matchingType]
  return valueType === VALUE_TYPE.multiple
}

let factorCodeIndex = 10
const genFactorCode = () => {
  return `A10${factorCodeIndex++}`
}

const factorValueCodeData = {
  A1001: 10,
  A1002: 10,
  A1003: 10,
  A1004: 10,
  A1005: 10,
  A1006: 10,
  A1007: 10
}
const genFactorValuedCode = (factorCode) => {
  let currentIndex = factorValueCodeData[factorCode] ?? 10
  currentIndex++
  factorValueCodeData[factorCode] = currentIndex

  return `${factorCode}_${currentIndex}`
}

export {
  getFactorList,
  initFactorList,
  clearFactorList,
  updateFactorList,
  getFactorWithCode,
  updateFactor,
  getFactorValueWithCode,
  updateFactorValue,
  addFactorValue,
  deleteFactorValue,
  getMatchingTypes,
  isMultipleValue,
  genFactorCode,
  genFactorValuedCode
}
